<?php
namespace app\api\controller\v2\model\Doctor;

use app\api\controller\Send;
use think\Model;
use think\Db;
use think\Request;
use think\Response;
use app\api\controller\UnauthorizedException;
use think\Validate;
use think\Image;
use JPush\Client as JPush;


class Doctor extends model
{
    use Send;
    //查看医生名字是否重复
    public function check_doctor_exist($user_name){
        $result = Db::query("select * from doctor_user where doctor_name = '$user_name'");
        return $result;
    }

    public function check_doctor_exist_id($doctor_id){
        $result = Db::query("select doctor_name from doctor_user where doctor_id = '$doctor_id'");
        return $result;
    }

    //查看医生电话是否被注册过
    public function check_doctor_name_exist($mobilephone){
        $result = Db::query("select * from doctor_user where mobilephone = '$mobilephone'");
        //halt();
        return $result;
    }

    public function check_login_status($mobilephone){
        $status = Db::query("select login_status from doctor_user where mobilephone = '$mobilephone'");
        return $status;
    }

    public function update_login_status($mobilephone){
        Db::query("update doctor_user set login_status = 1 where mobilephone = '$mobilephone'");
        return 1;
    }

    public function doctor_info_all($mobilephone){
        $result = Db::query("select * from doctor_user where mobilephone = '$mobilephone'");
        return $result;
    }

    public function update_access_token($mobilephone,$access_token){
           Db::query("update doctor_user set access_token = '$access_token' where mobilephone = '$mobilephone'");
        return true;
    }

    public function update_login_time($mobilephone){
        Db::query("update doctor_user set last_login_time = now() where mobilephone = '$mobilephone'");
        return true;
    }


    public function update_doctor_info($doctor_id,$doctor_name,$doctor_gender,$doctor_age,$path,$small_path,$licence,$age_limit,$idcard){
    Db::query("update doctor_user set doctor_name = '$doctor_name',doctor_gender = '$doctor_gender',doctor_age = '$doctor_age',doctor_icon= '$path',doctor_small_icon ='$small_path',licence='$licence',age_limit='$age_limit',idcard='$idcard' where doctor_id = '$doctor_id'");
    return true;
}



    public function update_doctor_approvement($doctor_id){
        $result = Db::query("select approve_or_not from doctor_user where doctor_id = '$doctor_id'");
        if($result[0]['approve_or_not'] == 0){
            Db::query("update doctor_user set approve_or_not = '1' where doctor_id = '$doctor_id'");
        }
    }
    //创建医生账户
    public function create_doctor_account($password,$confirm_password,$mobilephone){
        $doctor = new Doctor();
        if (!empty($mobilephone) && !empty($password)) {
            $result = $doctor -> check_doctor_name_exist($mobilephone);

            if(!empty($result)){
               //用户已存在
                return 1;

            }else if($password != $confirm_password) {
                //注册失败，密码不一致;
                return 2;

            }else if(strlen($password) < 8 || strlen($password) >16 ) {

                //注册失败，密码不规范，长度应在8~16位;
                return 3;

            }else {

                Db::query("insert into doctor_user (doctor_id,password,mobilephone,reg_date,last_login_time) values(null,md5('$password'),".$mobilephone.",now(),now())");
                return 4;
            }
                //注册失败，数据库插入错误
                return 5;

        }
    }


    /**
     * 改变医生登陆状态
     * @param $doctor_id
     * @param $status
     * @return array
     */
    public function change_logstas($doctor_id, $status){
        try{
            //更新数据库
            $result = Db::name('doctor_user')->update(['login_status'=>$status,'doctor_id'=>$doctor_id]);
        } catch(\Exception $e) {
            Log::write($e->getMessage(),'notice');
            return ['status'=>false,'message'=>$e->getMessage()];
        }
        return ['status'=>true,'message'=>'操作结束','eft'=>$result];
    }





}